// pages/home/home.js
import Notify from '@vant/weapp/notify/notify';
import Toast from '@vant/weapp/toast/toast';
import dayjs from 'dayjs';
import { actiStatEnum } from '../common/enums';
import { getFileType } from '../utils/file';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CustomBar: getApp().globalData.CustomBar,
    scrollHeight: '100vh',
    scrollTop: 0,
    // 主页图片轮播
    homeImgList: [],
    // 活动列表数据
    actiList: [],
  },
  // 点击图片
  onClickSwiperImage(data) {
    wx.navigateTo({
      url: '/pages/user/acti/userImg/userImg?actiId=' + data.detail.actiId,
    })
  },
  // 查询用户返图数据
  qryActiUserImg() {
    const qryParam = {}
    getApp().callContainer('/api/actiQuery/queryUserImg', qryParam, (res) => {
      if (res.data.code == 0) {
        const homeImgList = [];
        for (const userImg of res.data.data.userImgList) {
          const imgUrlList = userImg.imgUrlList.split(',');
          for (const imgUrl of imgUrlList) {
            const type = getFileType(imgUrl);
            if (homeImgList.length < 10) {
              homeImgList.push({ ...userImg, imgUrl: imgUrl, type: type });
            }
          }
        }

        for (let index = 0; index < homeImgList.length; index++) {
          const imgUrl = homeImgList[index];
          getApp().getTempFilePath(imgUrl.imgUrl).then((res) => {
            imgUrl.url = res;
            if (index == homeImgList.length - 1) {
              this.setData({ homeImgList: homeImgList });
            }
          })
        }
      }
    })
  },
  // 刷新活动列表数据
  qryActiList() {
    Toast.loading({
      message: '加载中...',
      forbidClick: true,
    });
    const qryParam = {}
    getApp().callContainer('/api/actiQuery/list', qryParam, (res) => {
      if (res.data.data.actiList != null) {
        for (const acti of res.data.data.actiList) {
          acti.actiStat_label = actiStatEnum.get(acti.actiStatus);
          acti.actiStat_color = this.getActiStatColor(acti.actiStatus);
          acti.imgUrlList = acti.actiImgUrlList.split(',');
          acti.actiBeginTime_label = dayjs(acti.actiBeginTime).format('YYYY-MM-DD HH:mm:ss');
        }
      }
      this.setData({ actiList: res.data.data.actiList || [] });
      this.loadTmpFile();
      Toast.clear();
    }, () => {
      Toast.clear();
    })
  },
  loadTmpFile() {
    // 加载商品图片
    for (const acti of this.data.actiList) {
      if (acti.imgUrlList) {
        acti.tmpUrlList = [];
        for (const imgUrl of acti.imgUrlList) {
          getApp().getTempFilePath(imgUrl).then((res) => {
            const type = getFileType(imgUrl);
            acti.tmpUrlList.push({ url: res, type: type });
            if (acti.tmpUrlList.length == acti.imgUrlList.length) {
              this.setData({ actiList: this.data.actiList });
            }
          })
        }
      }
    }
  },
  // 活动状态展示不同颜色
  getActiStatColor(actiStat) {
    if (actiStat == 2) {
      return 'bg-gradual-dd-orange';
    } else if (actiStat == 3) {
      return 'bg-gradual-dd-green';
    } else if (actiStat == 4) {
      return 'bg-gradual-dd-purple';
    } else if (actiStat == 5) {
      return 'bg-gradual-dd-red';
    } 
  },
  onFocusSearch() {
    wx.navigateTo({
      url: '/pages/home/search/search',
    })
  },
  onClickActiDetl(event) {
    const data = event.currentTarget.dataset.operation;
    wx.navigateTo({
      url: '/pages/user/acti/detail/detail?actiId=' + data.actiId,
    })
  },
  onScroll(event) {
    let isScrollEnd = false;
    if (event.detail.scrollTop > 10) {
      isScrollEnd = true;
    }
    if (isScrollEnd != this.data.isScrollEnd) {
      this.setData({ isScrollEnd: isScrollEnd })
    }
    this.setData({ scrollTop: event.detail.scrollTop });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.qryActiList();
    this.qryActiUserImg();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    // this.qryActiList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    // this.qryActiList(true);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  /**
   * 点击Tab刷新页面
   */
  onTabItemTap() {
    this.qryActiList();
    this.qryActiUserImg();
  }
})